package com.song.tools.log.operatelog.core;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.HashMap;
import java.util.Map;

/**
 * 日志记录上下文
 *
 * @author song tools
 * @since 2024-07-03
 */
public class LogRecordContext {

    private static final ThreadLocal<Map<String, String>> LOG_RECORD_MAP = new TransmittableThreadLocal<>();

    public static void put(String key, String value) {
        Map<String, String> map = get();
        map.put(key, value);
        LOG_RECORD_MAP.set(map);
    }

    public static Map<String, String> get() {
        Map<String, String> map = LOG_RECORD_MAP.get();
        if (map == null) {
            map = new HashMap<>();
        }
        return map;
    }

    public static void clean() {
        LOG_RECORD_MAP.remove();
    }
}
